package alpha2;


import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class User extends Common{
	
	public static void add(String tag) throws Exception {
        System.out.println("Start POST /user 创建用户 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/user");
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("tag", new StringBody(tag,Charset.forName("UTF-8")));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	public static void get(Integer skip, Integer limit) throws Exception{
		System.out.println("Start GET /user 获取所有用户 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/user" + "?skip=" + skip + "&limit=" + limit);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void get(String userId) throws Exception{
		System.out.println("Start GET /user/{user_id} 获取单个用户 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/user/" + userId);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void delete(String userId) throws Exception{
		System.out.println("Start DELETE /user/{user_id} 删除某个用户 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete request = new HttpDelete(HOST + "/user/" + userId);

        //发起网络请求
        httpclient.execute(request);
        
	}
	
	
	public static void update(String userId, String tag) throws Exception {
        System.out.println("Start PUT /user/{user_id} 修改某个用户 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut request = new HttpPut(HOST + "/user/" + userId);
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("tag", new StringBody(tag,Charset.forName("UTF-8")));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	

	public static void image(String userId, Double qualityThreshold, String image) throws Exception {
        System.out.println("Start POST /user/{user_id}/image 向用户加入图片 ...");
        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost request = new HttpPost(HOST + "/user/" + userId + "/image");
        
        MultipartEntity reqEntity = new MultipartEntity();  
        reqEntity.addPart("quality_threshold", new StringBody(qualityThreshold.toString()));
        reqEntity.addPart("image", new FileBody(new File(image)));
        request.setEntity(reqEntity); 

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
    }
	
	
	public static void imageDelete(String userId, String imageId) throws Exception{
		System.out.println("Start DELETE /user/{user_id}/image/{image_id} 删除用户的图片 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpDelete request = new HttpDelete(HOST + "/user/" + userId + "/image/" + imageId);

        //发起网络请求
        httpclient.execute(request);
        
	}
	
	@SuppressWarnings("deprecation")
	public static void get(String query, Integer skip, Integer limit) throws Exception{
		System.out.println("Start GET /user_query 查询用户 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/user_query" + "?query=" + URLEncoder.encode(query) + "&skip=" + skip + "&limit=" + limit);

        //发起网络请求 获取返回信息
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity());
        
        System.out.println(responseBody);
	}
	
	public static void getImage(String imageId) throws Exception{
		System.out.println("Start GET /image/{image_id} 获得图片 ...");
	        
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpGet request = new HttpGet(HOST + "/image/" + imageId);

        //发起网络请求 获取返回信息 
        CloseableHttpResponse response = httpclient.execute(request);
        // 得到网络资源的字节数组,并写入文件  
        HttpEntity entity = response.getEntity();  
        if (entity != null) {  
            InputStream instream = entity.getContent();  
            FileOutputStream output = new FileOutputStream(new File("src/test/resources/" + imageId + ".jpg")); 
            byte b[] = new byte[1024];  
            int j = 0;  
            while( (j = instream.read(b))!=-1){  
                output.write(b,0,j);  
            }  
            output.flush();  
            output.close();  
            instream.close();
        }  
	}
	
	

}
